﻿#include <QMainWindow>
#include <QApplication>
#include <QScreen>

#include "customwindow.h"

QMainWindow *Custom::newModalWindow(const QString &title, QWidget *child, QObject *del, bool disableMin)
{
    QMainWindow *win = new QMainWindow(nullptr, Qt::WindowStaysOnTopHint);
//    win->setWindowIcon(Settings::logo());
    win->setAttribute(Qt::WA_QuitOnClose, false);
    win->setWindowTitle(title);
    win->setWindowModality(Qt::ApplicationModal);
    win->setMinimumSize(child->size());
    win->resize(child->size());
    QObject::connect(qApp, &QApplication::aboutToQuit, win, &QMainWindow::close);
    if (disableMin)
    {
        win->setWindowFlags(win->windowFlags() & ~Qt::WindowMinimizeButtonHint);
    }

    QRect desktop = qApp->screens().first()->availableGeometry();
    QRect geo(QPoint(0, 0), child->size());
    geo.moveCenter(desktop.center());
    win->setGeometry(geo);
    win->setCentralWidget(child);

    if (del != nullptr)
    {
        QObject::connect(del, &QObject::destroyed, win, &QMainWindow::deleteLater);//自动析构
    }

    return win;
}
